/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package name.coolwind.cxblog.util;

/**
 *
 * @author coolwind
 */
public class Pagination {

    private int totalRows;
    private int curPage;
    private int numLinks;
    private int perPage;
    private int nextpage;
    private int prevPage;
    private int numPages;
    private int start;
    private int end;

    public Pagination() {

    }

    public Pagination(int totalRows, int curPage, int perPage, int numLinks) {
        this.totalRows = totalRows;
        this.curPage = curPage;
        this.perPage = perPage;
        this.numLinks = numLinks;

        this.compute();
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getNumLinks() {
        return numLinks;
    }

    public void setNumLinks(int numLinks) {
        this.numLinks = numLinks;
    }

    public int getPerPage() {
        return perPage;
    }

    public void setPerPage(int perPage) {
        this.perPage = perPage;
    }

    public int getNextpage() {
        return nextpage;
    }

    public void setNextpage(int nextpage) {
        this.nextpage = nextpage;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public void setPrevPage(int prevPage) {
        this.prevPage = prevPage;
    }

    public int getNumPages() {
        return numPages;
    }

    public void setNumPages(int numPages) {
        this.numPages = numPages;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public void compute() {
        nextpage = curPage;
        prevPage = curPage;

        if (perPage != 0 && totalRows != 0) {
            numPages = totalRows / perPage;
            if (totalRows % perPage != 0) {
                numPages++;
            }

            if (numPages > 1) {
                if (curPage > 0) {
                    prevPage--;
                }

                if (curPage < numPages - 1) {
                    nextpage++;
                }

                start = curPage;
                end = curPage;

                while (start > 0 && curPage - start < numLinks / 2) {
                    start--;
                    end++;
                }

                while (end - start < numLinks && end < numPages) {
                    end++;
                }

                while (end > numPages) {
                    end--;
                }

                while (end - start < numLinks && start > 0) {
                    start--;
                }
            }
        }
    }
}
